﻿Imports System.Data
Public Class SysManage
    Protected Shared Ds As DataSet = New DataSet
    Public Shared Function passwordmanage(ByVal ID As String, ByVal OldPassWord As String, ByVal NewPassWord As String) As Boolean '修改密码
        Dim Ret As Boolean = False
        Ds.Clear()
        Ds = GlobalClass.SQLclass.Sqlcmd("select 员工号 from UserInfo where 员工号='" + ID + "' and 登录密码='" + Crypt.MD5(OldPassWord) + "'")
        If (Ds.Tables(0).Rows.Count = 1) Then
            GlobalClass.SQLclass.Sqlcmd("update UserInfo set 登录密码='" + Crypt.MD5(NewPassWord) + "' where 员工号='" + ID + "'")
            Ret = True
        Else
            Throw New Exception("旧用户名或旧密码错误!")
        End If
        Return Ret
    End Function

    Public Shared Function usermanage(ByVal ID As String, ByVal PassWord As String, ByVal Level As Long) As Boolean '添加用户
        Dim Ret As Boolean = False
        Ds.Clear()
        Ds = GlobalClass.SQLclass.Sqlcmd("select 员工号 from UserInfo where 员工号='" + ID + "'")
        If (Ds.Tables(0).Rows.Count = 0) Then
            GlobalClass.SQLclass.Sqlcmd("INSERT INTO UserInfo VALUES ('" & ID & "','" + Crypt.MD5(PassWord) & "'," & Level & ")")
            If (CreateClerkInfo(ID) = True) Then
                Ret = True
            End If
        Else
            Throw New Exception("该员工号已存在!")
        End If
        Return Ret
    End Function

    Private Shared Function CreateClerkInfo(ByVal ID As String) As Boolean '添加员工信息
        Dim Ret As Boolean = False
        Ds.Clear()
        GlobalClass.SQLclass.Sqlcmd("INSERT INTO ClerkInfo VALUES ('" & ID & "','',0,0,'','',0,'" & DateTime.Now & "')")
        Ds = GlobalClass.SQLclass.Sqlcmd("select * from ClerkInfo where 员工编号='" & ID & "'")
        If (Ds.Tables(0).Rows.Count = 1) Then
            Ret = True
        Else
            Throw New Exception("添加员工信息失败!")
        End If
        Return Ret
    End Function

    Public Shared Function usermanage(ByVal ID As String) As Boolean '删除用户
        Dim Ret As Boolean = False
        Ds.Clear()
        Ds = GlobalClass.SQLclass.Sqlcmd("select 员工号 from UserInfo where 员工号='" & ID & "'")
        If (Ds.Tables(0).Rows.Count = 1) Then
            If (DeleteClerkInfo(ID) = True) Then
                GlobalClass.SQLclass.Sqlcmd("delete from UserInfo where 员工号='" & ID & "'")
            Else
                Throw New Exception("删除该用户失败!")
            End If
            Ret = True
        Else
            Throw New Exception("未找到该用户!")
        End If
        Return Ret
    End Function

    Private Shared Function DeleteClerkInfo(ByVal ID As String) As Boolean '删除员工信息
        Dim Ret As Boolean = False
        Ds.Clear()
        GlobalClass.SQLclass.Sqlcmd("delete from ClerkInfo where 员工编号='" & ID & "'")
        If (Ds.Tables(0).Rows.Count = 0) Then
            Ret = True
        Else
            Throw New Exception("删除员工信息失败!")
        End If
        Return Ret
    End Function

    Public Shared Function menumanage(ByVal Code As String, ByVal Name As String, ByVal Price As Double) As Boolean '增加菜单
        Dim Ret As Boolean = False
        Ds.Clear()
        Ds = GlobalClass.SQLclass.Sqlcmd("select 菜单编码 from MenuInfo where 菜单编码='" + Code + "'")
        If (Ds.Tables(0).Rows.Count = 0) Then
            GlobalClass.SQLclass.Sqlcmd("insert into MenuInfo values('" & Code & "','" & Name & "'," & Price & ")")
            Ret = True
        Else
            Throw New Exception("该菜单编码已存在!")
        End If
        Return Ret
    End Function

    Public Shared Function menumanage(ByVal Code As String) As Boolean '删除菜单
        Dim Ret As Boolean = False
        Ds.Clear()
        Ds = GlobalClass.SQLclass.Sqlcmd("select 菜单编码 from MenuInfo where 菜单编码='" + Code + "'")
        If (Ds.Tables(0).Rows.Count = 1) Then
            GlobalClass.SQLclass.Sqlcmd("delete from MenuInfo where 菜单编码='" & Code & "'")
            Ret = True
        Else
            Throw New Exception("该菜单编码不存在!")
        End If
        Return Ret
    End Function

    Public Shared Sub tablemanage(ByVal No As Long, ByVal Count As Long) '增加餐桌
        Ds.Clear()
        Ds = GlobalClass.SQLclass.Sqlcmd("SELECT * FROM TableInfo")
        Ds.Clear()
        Ds = GlobalClass.SQLclass.Sqlcmd("SELECT * FROM TableInfo")
        If Ds.Tables(0).Rows.Count < 190 Then
            GlobalClass.SQLclass.Sqlcmd("insert into TableInfo values(" + No.ToString + "," + Count.ToString + ",0,null,null)")
        Else
            Throw New Exception("只能存在190张桌子")
        End If
    End Sub

    Public Shared Sub tablemanage(ByVal No As Long) '删除餐桌
        Ds.Clear()
        Ds = GlobalClass.SQLclass.Sqlcmd("SELECT * FROM TableInfo WHERE 餐桌号=" + No.ToString)
        Ds.Clear()
        Ds = GlobalClass.SQLclass.Sqlcmd("SELECT * FROM TableInfo WHERE 餐桌号=" + No.ToString)
        If Ds.Tables(0).Rows.Count = 0 Then Throw New Exception("餐桌号不存在!")
        GlobalClass.SQLclass.Sqlcmd("delete from TableInfo where 餐桌号=" & No.ToString)
    End Sub
End Class
